﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Config.Internal;
//using LocalizationTool;
using System;
using System.Reflection;

public class ConfigManager : Singleton<ConfigManager>
{
    //key 为data type
    private Dictionary<System.Type, ConfigBase> _configMaps = new Dictionary<System.Type, ConfigBase>();
    //key 为config type
    private Dictionary<System.Type, ConfigBase> _configMaps0 = new Dictionary<System.Type, ConfigBase>();

    //private LocalizeManager localizeManager;

    public override void Initialize()
    {
        LoadConfig();
        //localizeManager = LocalizeManager.Instance;
        //localizeManager.Initialize();
    }

    public void Clear()
    {
        _configMaps.Clear();
        _configMaps0.Clear();
    }

    private void LoadConfig()
    {
        Assembly assembly = Assembly.GetAssembly(typeof(ConfigDataBase));
        Type[] typeArr = assembly.GetExportedTypes();
        List<Type> typelist = new List<Type>();
        for (int i = 0; i < typeArr.Length; i++)
        {
            object[] objs = typeArr[i].GetCustomAttributes(typeof(ConfigDataAttribute), false);
            if (objs.Length > 0)
            {
                typelist.Add(typeArr[i]);
            }
        }
        for (int i = 0; i < typelist.Count; i++)
        {
            ConfigBase config = ConfigLoadTool.LoadConfig(typelist[i]);
            if (config != null)
            {
                _configMaps.Add(typelist[i], config);
            }
        }
    }

    /// <summary>
    /// 获取配置类
    /// </summary>
    public T GetConfig<T>() where T : ConfigBase
    {
        if (_configMaps0.TryGetValue(typeof(T), out var config))
        {
            return config as T;
        }
        foreach (var item in _configMaps.Values)
        {
            if (item is T)
            {
                _configMaps0.Add(typeof(T), item);
                return item as T;
            }
        }
        return null;
    }

    /// <summary>
    /// 获取一个T类型配置数据
    /// </summary>
    public T GetData<T>(uint id) where T : ConfigDataBase
    {
        if (_configMaps.TryGetValue(typeof(T), out var config))
        {
            return (config as ConfigBase<T>).GetData(id);
        }
        return null;
    }

    /// <summary>
    /// 获取所有T类型配置数据
    /// </summary>
    public List<T> GetDataList<T>() where T : ConfigDataBase
    {
        if (_configMaps.TryGetValue(typeof(T), out var config))
        {
            return (config as ConfigBase<T>).GetDataList();
        }
        return null;
    }

    //public string GetTextContent(int id)
    //{
    //    return localizeManager.GetTextContent(id);
    //}

    //public Sprite GetSpriteContent(int id)
    //{
    //    return localizeManager.GetSpriteContent(id);
    //}

}
